using GTA;
using GTA.Native;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Reflection;
using System.Windows.Forms;

public class AngryDrivers : Script
{
    bool Activated = false;
    bool Bicycle = false;
    bool Bike = false;
    bool Boat = false;
    bool Car = true;
    bool Cargobob = false;
    bool Helicopter = false;
    bool Plane = false;
    bool Quadbike = false;
    bool Train = false;
    bool MenuIsVisible = false;
    Color HeaderTextColor = Color.FromArgb(230, 255, 255, 255);
    Color HeaderColor = Color.FromArgb(230, 255, 0, 0);
    Color SelectedItemColor = Color.FromArgb(230, 255, 0, 0);
    Color SelectedTextColor = Color.FromArgb(230, 255, 255, 255);
    Color UnselectedItemColor = Color.FromArgb(230, 255, 218, 143);
    Color UnselectedTextColor = Color.FromArgb(230, 0, 0, 0);
    Color FooterColor = Color.FromArgb(230, 255, 0, 0);
    Color FooterTextColor = Color.FromArgb(230, 255, 255, 255);
    Keys MenuKey = Keys.F5;
    float Speed = 70.0f;
    int CurrentUpdateInterval = 200;
    int MenuWidth = 400;
    int UpdateInterval = 200;

    public AngryDrivers()
    {
        LoadSettings();
        Tick += OnTick;
        KeyUp += OnKeyUp;
    }

    private void OnTick(object sender, EventArgs e)
    {
        if (Activated)
        {
            if (CurrentUpdateInterval < UpdateInterval)
            {
                CurrentUpdateInterval += 18;
            }
            else
            {
                Drive();
                CurrentUpdateInterval = 0;
            }
        }
    }

    void Drive()
    {
        foreach (Vehicle Car in World.GetAllVehicles())
        {
            bool istagettingplayer = false;
            if (Game.Player.Character.IsInVehicle())
            {
                if (Car != Game.Player.Character.CurrentVehicle)
                {
                    istagettingplayer = CheckVehicleType(Car);
                }
            }
            else
            {
                istagettingplayer = CheckVehicleType(Car);
            }
            if (istagettingplayer)
            {
                Car.GetPedOnSeat(VehicleSeat.Driver).Task.DriveTo(Car, Game.Player.Character.Position, 0, Speed);
            }
        }
    }

    bool CheckVehicleType(Vehicle CarToCheck)
    {
        bool targettingenabled = false;
        if (CarToCheck.Model.IsBicycle)
        {
            if (Bicycle)
            {
                targettingenabled = true;
            }
        }
        else if (CarToCheck.Model.IsBike)
        {
            if (Bike)
            {
                targettingenabled = true;
            }
        }
        else if (CarToCheck.Model.IsBoat)
        {
            if (Boat)
            {
                targettingenabled = true;
            }
        }
        else if (CarToCheck.Model.IsCar)
        {
            if (Car)
            {
                targettingenabled = true;
            }
        }
        else if (CarToCheck.Model.IsCargobob)
        {
            if (Cargobob)
            {
                targettingenabled = true;
            }
        }
        else if (CarToCheck.Model.IsHelicopter)
        {
            if (Helicopter)
            {
                targettingenabled = true;
            }
        }
        else if (CarToCheck.Model.IsPlane)
        {
            if (Plane)
            {
                targettingenabled = true;
            }
        }
        else if (CarToCheck.Model.IsTrain)
        {
            if (Train)
            {
                targettingenabled = true;
            }
        }
        return targettingenabled;
    }

    void OnKeyUp(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == MenuKey)
        {
            if (MenuIsVisible == false)
            {
                ShowMenu();
                MenuIsVisible = true;
            }
            else
            {
                View.CloseAllMenus();
                MenuIsVisible = false;
            }
        }
    }

    void ShowMenu()
    {
        View.CloseAllMenus();
        GTA.MenuToggle ToggleAngryDriversButton = new MenuToggle("Toggle Angry Drivers", "Toggle Angry Drivers", Activated);
        GTA.MenuNumericScroller SpeedButton = new MenuNumericScroller("Speed", "Speed", 0, 999999, 1, (int)Speed);
        GTA.MenuNumericScroller IntervalButton = new MenuNumericScroller("Driver Targetting Update Interval", "Driver Targetting Update Interval\n(in Milliseconds)", 0, 999999, 100, UpdateInterval / 100);
        GTA.MenuToggle ToggleAngryBicycleDriversButton = new MenuToggle("Bicycle", "Toggle Angry Bicycle-Drivers", Bicycle);
        GTA.MenuToggle ToggleAngryBikeDriversButton = new MenuToggle("Bike", "Toggle Angry Bike-Drivers", Bike);
        GTA.MenuToggle ToggleAngryBoatDriversButton = new MenuToggle("Boat", "Toggle Angry Boat-Drivers", Boat);
        GTA.MenuToggle ToggleAngryCarDriversButton = new MenuToggle("Car", "Toggle Angry Car-Drivers", Car);
        GTA.MenuToggle ToggleAngryCargobobDriversButton = new MenuToggle("Cargobob", "Toggle Angry Cargobob-Drivers", Cargobob);
        GTA.MenuToggle ToggleAngryHelicopterDriversButton = new MenuToggle("Helicopter", "Toggle Angry Helicopter-Drivers", Helicopter);
        GTA.MenuToggle ToggleAngryPlaneDriversButton = new MenuToggle("Plane", "Toggle Angry Plane-Drivers", Plane);
        GTA.MenuToggle ToggleAngryQuadbikeDriversButton = new MenuToggle("Quadbike", "Toggle Angry Quadbike-Drivers", Quadbike);
        GTA.MenuToggle ToggleAngryTrainDriversButton = new MenuToggle("Train", "Toggle Angry Train-Drivers", Train);
        GTA.MenuButton LoadSettingsButton = new MenuButton("Load Settings", "Load Settings");
        GTA.MenuButton SaveSettingsButton = new MenuButton("Save Settings", "Save Settings");
        GTA.Menu ShownMenu = new GTA.Menu("Angry Drivers", new GTA.IMenuItem[] { ToggleAngryDriversButton, SpeedButton, IntervalButton, ToggleAngryBicycleDriversButton, ToggleAngryBikeDriversButton, ToggleAngryBoatDriversButton, ToggleAngryCarDriversButton, ToggleAngryCargobobDriversButton, ToggleAngryHelicopterDriversButton, ToggleAngryPlaneDriversButton, ToggleAngryQuadbikeDriversButton, ToggleAngryTrainDriversButton, LoadSettingsButton, SaveSettingsButton });
        ToggleAngryDriversButton.Changed += (sender, args) => Activated = !Activated;
        SpeedButton.Changed += (sender, args) => Speed = (float) SpeedButton.Value;
        IntervalButton.Changed += (sender, args) => UpdateInterval = (int) IntervalButton.Value;
        ToggleAngryBicycleDriversButton.Changed += (sender, args) => Bicycle = !Bicycle;
        ToggleAngryBikeDriversButton.Changed += (sender, args) => Bike = !Bike;
        ToggleAngryBoatDriversButton.Changed += (sender, args) => Boat = !Boat;
        ToggleAngryCarDriversButton.Changed += (sender, args) => Car = !Car;
        ToggleAngryCargobobDriversButton.Changed += (sender, args) => Cargobob = !Cargobob;
        ToggleAngryHelicopterDriversButton.Changed += (sender, args) => Helicopter = !Helicopter;
        ToggleAngryPlaneDriversButton.Changed += (sender, args) => Plane = !Plane;
        ToggleAngryQuadbikeDriversButton.Changed += (sender, args) => Quadbike = !Quadbike;
        ToggleAngryTrainDriversButton.Changed += (sender, args) => Train = !Train;
        LoadSettingsButton.Activated += (sender, args) => LoadSettings();
        SaveSettingsButton.Activated += (sender, args) => SaveSettings();
        ShownMenu.HeaderTextColor = HeaderTextColor;
        ShownMenu.HeaderColor = HeaderColor;
        ShownMenu.SelectedItemColor = SelectedItemColor;
        ShownMenu.SelectedTextColor = SelectedTextColor;
        ShownMenu.UnselectedItemColor = UnselectedItemColor;
        ShownMenu.UnselectedTextColor = UnselectedTextColor;
        ShownMenu.FooterColor = FooterColor;
        ShownMenu.FooterTextColor = FooterTextColor;
        ShownMenu.Width = MenuWidth;
        View.AddMenu(ShownMenu);
    }

    void LoadSettings()
    {
        if (File.Exists(AppDomain.CurrentDomain.BaseDirectory + "\\AngryDriversConfig.txt"))
        {
            string[] Line = File.ReadAllText(AppDomain.CurrentDomain.BaseDirectory + "\\AngryDriversConfig.txt").Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
            if (Line.Length == 19)
            {
                Bicycle = bool.Parse(Line[0].Split('=')[1]);
                Bike = bool.Parse(Line[1].Split('=')[1]);
                Boat = bool.Parse(Line[2].Split('=')[1]);
                Car = bool.Parse(Line[3].Split('=')[1]);
                Cargobob = bool.Parse(Line[4].Split('=')[1]);
                Helicopter = bool.Parse(Line[5].Split('=')[1]);
                Plane = bool.Parse(Line[6].Split('=')[1]);
                Quadbike = bool.Parse(Line[7].Split('=')[1]);
                Train = bool.Parse(Line[8].Split('=')[1]);
                MenuKey = (Keys)Enum.Parse(typeof(Keys), Line[9].Split('=')[1]);
                Speed = float.Parse(Line[10].Split('=')[1]);
                UpdateInterval = int.Parse(Line[11].Split('=')[1]);
                HeaderTextColor = Color.FromArgb(int.Parse(Line[12].Split('=')[1].Split(',')[0]), int.Parse(Line[12].Split('=')[1].Split(',')[1]), int.Parse(Line[12].Split('=')[1].Split(',')[2]), int.Parse(Line[12].Split('=')[1].Split(',')[3]));
                HeaderColor = Color.FromArgb(int.Parse(Line[13].Split('=')[1].Split(',')[0]), int.Parse(Line[13].Split('=')[1].Split(',')[1]), int.Parse(Line[13].Split('=')[1].Split(',')[2]), int.Parse(Line[13].Split('=')[1].Split(',')[3]));
                SelectedItemColor = Color.FromArgb(int.Parse(Line[14].Split('=')[1].Split(',')[0]), int.Parse(Line[14].Split('=')[1].Split(',')[1]), int.Parse(Line[14].Split('=')[1].Split(',')[2]), int.Parse(Line[14].Split('=')[1].Split(',')[3]));
                SelectedTextColor = Color.FromArgb(int.Parse(Line[15].Split('=')[1].Split(',')[0]), int.Parse(Line[15].Split('=')[1].Split(',')[1]), int.Parse(Line[15].Split('=')[1].Split(',')[2]), int.Parse(Line[15].Split('=')[1].Split(',')[3]));
                UnselectedItemColor = Color.FromArgb(int.Parse(Line[14].Split('=')[1].Split(',')[0]), int.Parse(Line[14].Split('=')[1].Split(',')[1]), int.Parse(Line[14].Split('=')[1].Split(',')[2]), int.Parse(Line[14].Split('=')[1].Split(',')[3]));
                UnselectedTextColor = Color.FromArgb(int.Parse(Line[15].Split('=')[1].Split(',')[0]), int.Parse(Line[15].Split('=')[1].Split(',')[1]), int.Parse(Line[15].Split('=')[1].Split(',')[2]), int.Parse(Line[15].Split('=')[1].Split(',')[3]));
                FooterColor = Color.FromArgb(int.Parse(Line[16].Split('=')[1].Split(',')[0]), int.Parse(Line[16].Split('=')[1].Split(',')[1]), int.Parse(Line[16].Split('=')[1].Split(',')[2]), int.Parse(Line[16].Split('=')[1].Split(',')[3]));
                FooterTextColor = Color.FromArgb(int.Parse(Line[17].Split('=')[1].Split(',')[0]), int.Parse(Line[17].Split('=')[1].Split(',')[1]), int.Parse(Line[17].Split('=')[1].Split(',')[2]), int.Parse(Line[17].Split('=')[1].Split(',')[3]));
                MenuWidth = int.Parse(Line[18].Split('=')[1]);
            }
        }
    }

    string ColorToString(Color ColorToConvert)
    {
        return ColorToConvert.ToString().Replace("(", "").Replace("Color [A=", "").Replace(" R=", "").Replace(" G=", "").Replace(" B=", "").Replace("]", "");
    }

    void SaveSettings()
    {
        File.WriteAllText(AppDomain.CurrentDomain.BaseDirectory + "\\AngryDriversConfig.txt", "Bicycle=" + Bicycle.ToString() + Environment.NewLine + "Bike=" + Bike.ToString() + Environment.NewLine + "Boat=" + Boat.ToString() + Environment.NewLine + "Car=" + Car.ToString() + Environment.NewLine + "Cargobob=" + Cargobob.ToString() + Environment.NewLine + "Helicopter=" + Helicopter.ToString() + Environment.NewLine + "Plane=" + Plane.ToString() + Environment.NewLine + "Quadbike=" + Quadbike.ToString() + Environment.NewLine + "Train=" + Train.ToString() + Environment.NewLine + "MenuKey=" + MenuKey.ToString() + Environment.NewLine + "Speed=" + Speed.ToString() + Environment.NewLine + "UpdateInterval=" + UpdateInterval.ToString() + Environment.NewLine + "HeaderTextColor=" + ColorToString(HeaderTextColor) + Environment.NewLine + "HeaderColor=" + ColorToString(HeaderColor) + Environment.NewLine + "SelectedItemColor=" + ColorToString(SelectedItemColor) + Environment.NewLine + "SelectedTextColor=" + ColorToString(SelectedTextColor) + Environment.NewLine + "UnselectedItemColor=" + ColorToString(UnselectedItemColor) + Environment.NewLine + "UnselectedTextColor=" + ColorToString(UnselectedTextColor) + Environment.NewLine + "FooterColor=" + ColorToString(FooterColor) + Environment.NewLine + "FooterTextColor=" + ColorToString(FooterTextColor) + Environment.NewLine + "MenuWidth=" + MenuWidth.ToString());
    }
}